using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace CSP.WebPartPages.Webpart
{
    [Guid("8fbbdd8d-c450-45ac-b3e5-d4f2792512b6")]
    public class FollowerRequests : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myListName = null;
        private string _domain = null;
        private string _CSP = "~/_controltemplates/CSP/FollowerRequests.ascx";


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Peeps")]
        [WebDisplayName("List Name")]
        [WebDescription("List Name witch contains the follower requests.")]
        public string MyListName
        {
            get
            {
                if (_myListName == null)
                {
                    _myListName = "Follower requests";
                }
                return _myListName;
            }
            set { _myListName = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Peeps")]
        [WebDisplayName("Domain")]
        [WebDescription("Domain")]
        public string Domain
        {
            get
            {
                if (_domain == null)
                {
                    _domain = "SIEENA";
                }
                return _domain;
            }
            set { _domain = value; }
        }

        public FollowerRequests()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();
                    CSP.WebControls.FollowerRequests frControl = (CSP.WebControls.FollowerRequests)this.Page.LoadControl(_CSP);
                    frControl.MyListName = MyListName;
                    frControl.Domain = Domain;
                    this.Controls.Add(frControl);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
